package com.swarovski.rmi;

import java.rmi.AccessException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RMISocketFactory;

import org.tanukisoftware.wrapper.WrapperListener;
import org.tanukisoftware.wrapper.WrapperManager;

/**
 * The Class Server.
 */
public class Server implements WrapperListener {

	/**
	 * Instantiates a new server.
	 */
	public Server() {
		// Standard-Konstruktor
	}

	/**
	 * Instantiates a new server.
	 *
	 * @param port the port
	 *
	 * @throws Exception the exception
	 */
	public Server(int port) throws Exception {
		// Variablen initialisieren
		String serviceName = "TestRmiService";

		RMISocketFactory.setSocketFactory(new FixedPortRMISocketFactory());

		ServerImpl obj = new ServerImpl();
		try {

			if (System.getSecurityManager() == null) {
	            System.setSecurityManager(new RMISecurityManager());
	        }

	        Registry reg = LocateRegistry.getRegistry(port);

	        boolean bound = false;

	        for (int i = 0; !bound && i < 2; i++) {
	            try {
	                reg.rebind(serviceName, obj);
	                bound = true;
	                System.out.println(serviceName + " gebunden an Registry, Port " + port + ".");
	            } catch (RemoteException e) {
	            	System.out.println("Rebinding " + serviceName + " fehlgeschlagen, versuche nochmals ...");
	                reg = LocateRegistry.createRegistry(port);
	                System.out.println("Registry an Port " + port + " gestartet.");
	            }
	        }
	        System.out.println("TestRmiService ready...");

		} catch (AccessException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
		// Start the application. If the JVM was launched from the native
		// Wrapper then the application will wait for the native Wrapper to
		// call the application's start method. Otherwise the start method
		// will be called immediately.
		WrapperManager.start(new Server(), args);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.tanukisoftware.wrapper.WrapperListener#controlEvent(int)
	 */
	public void controlEvent(int event) {
		if (WrapperManager.isControlledByNativeWrapper()) {
			// The Wrapper will take care of this event
		} else {
			// We are not being controlled by the Wrapper, so
			// handle the event ourselves.
			if ((event == WrapperManager.WRAPPER_CTRL_C_EVENT)
					|| (event == WrapperManager.WRAPPER_CTRL_CLOSE_EVENT)
					|| (event == WrapperManager.WRAPPER_CTRL_SHUTDOWN_EVENT)) {
				WrapperManager.stop(0);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.tanukisoftware.wrapper.WrapperListener#start(java.lang.String[])
	 */
	public Integer start(String[] args) {
		int port = (args.length > 0) ? Integer.parseInt(args[0]) : 4711;
		try {
			new Server(port);
		} catch (Exception e) {
			System.out.println("Exception TestRmiService: " + e);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.tanukisoftware.wrapper.WrapperListener#stop(int)
	 */
	public int stop(int arg0) {
		return 0;
	}
}